var SERVER_PORT = "http://120.77.223.88:8080/red/";
var userAddress = new Vue({
    el: '#app-userAddress',
    data: {
        //登录账户
        isLogin:false,
        phone: "",
        nickname:"",

        addresses:[],

        msg:"请尽量填写详细地址",

        //添加地址
        name:"",
        address:"",
        tel:"",

        //控制显示属性
        showAddAddress: false,
        showAddBtn: true,

         //得到所有目录
         primaryCategory: [],
    },
    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);
        },

        //获取订单
        getUserAddress: function () {
            this.$http.post(SERVER_PORT + "requestUserAddress", {
                user_phone: this.phone
            }, {emulateJSON: true})
                .then((res) => {
                    if(res.data.status === 1){
                        console.log(res.data.data);
                        this.addresses = res.data.data;
                        if(this.addresses.length === 0)
                            this.msg = "地址栏空空如也，赶快添加地址吧~"
                    } else {
                        alert("获取数据失败");
                    }
                }, (err) => {
                    alert("获取数据失败");
                });
        },

        openAddAddress: function () {
            this.showAddAddress = true;
            this.showAddBtn = false;
        },

        hideAddAddress: function () {
            this.showAddAddress = false;
            this.showAddBtn = true;
        },

        //添加地址
        addAddress: function () {
            console.log("name:"+this.name+", address:"+this.address+", phone:"+this.tel);
            if(this.name != null && this.address && this.tel){
                this.$http.post(SERVER_PORT + "addUserAddress", {
                    address: this.address,
                    rec_name: this.name,
                    rec_phone: this.tel,
                    user_phone: this.phone
                }, {emulateJSON: true})
                    .then((res) => {
                        console.log(res.data);
                        if (res.data.status == 1) {

                            userAddress.hideAddAddress();
                            //刷新
                            window.location.reload();
                        } else {
                            console.log("添加失败");
                            alert("添加失败");
                        }
                    }, (res) => {
                        alert("添加失败: " + res);
                    });
            }else {
                alert("内容不能为空");
            }
        },

        
        getAllCategory: function () {
            this.$http.get(SERVER_PORT + "requestCategory").then(
                response => {
                    //console.log(response.data.data);
                    var originalChaoticInfo = response.data.data;

                    for (var index in originalChaoticInfo) {
                        var primaryCategoryName = originalChaoticInfo[index].category1_Name;
                        var primaryCategoryID = originalChaoticInfo[index].category1_ID;

                        var primaryCategoryItem = {
                            name: primaryCategoryName,
                            ID: primaryCategoryID
                        }

                        // var secondCategory = [];

                        // for (var sub_index in originalChaoticInfo[index].category2) {
                        //     var secondCategoryName =
                        //         originalChaoticInfo[index].category2[sub_index].name;
                        //     var category2_ID =
                        //         originalChaoticInfo[index].category2[sub_index].category2_ID;
                        //     secondCategory.push({
                        //         name: secondCategoryName,
                        //         ID: category2_ID
                        //     });
                        // }

                        // this.categoryTree.push({
                        //     primaryCategory: primaryCategory,
                        //     secondCategory: secondCategory
                        // });
                        this.primaryCategory.push(primaryCategoryItem);
                    }

                },
                response => {
                    // 响应错误回调
                    alert("请求失败");
                }
            );
        },

        onPrimCateClick: function (index) {
            console.log(index);
            window.localStorage.setItem("primIndex_goodCart", index);
            window.location.href = "searchResult.html"
        },


    },

    created: function () {
        this.getAllCategory();
        if(window.localStorage.getItem("isLogin") === undefined || window.localStorage.getItem("isLogin") === "false"){
            alert("请先登录");
            window.location.href="login.html";
        }else{
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
            this.getUserAddress();
        }
    }
});